﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using ParserGenerator.GrammarConstruction;

namespace ParserGenerator.ParserConstruction
{
    [Serializable]
    public sealed class ParserAutomatonReductionLookaheadSets : ReadOnlyDictionary<GrammarProduction, ParserAutomatonReductionLookaheadSet>
    {
        public ParserAutomatonState State { get; }

        internal ParserAutomatonReductionLookaheadSets(ParserAutomatonState state)
            : this(state, false)
        {
        }

        internal ParserAutomatonReductionLookaheadSets(ParserAutomatonState state, bool lazy)
            : base(new Dictionary<GrammarProduction, ParserAutomatonReductionLookaheadSet>())
        {
            if (state == null)
                throw new ArgumentNullException(nameof(state));
            State = state;
            if (lazy)
                return;
            foreach (ParserAutomatonReduction reduce in state.Reductions.Values)
            {
                var set = new ParserAutomatonReductionLookaheadSet(reduce);
                Dictionary.Add(reduce.Production, set);
            }
        }

        internal void Add(GrammarProduction production, ParserAutomatonReductionLookaheadSet set)
        {
            if (production == null)
                throw new ArgumentNullException(nameof(production));
            if (set == null)
                throw new ArgumentNullException(nameof(set));
            Dictionary.Add(production, set);
        }
    }
}
